<?php
	ob_start();
	session_start();

	function __autoload($class_name) {
		include './'.$class_name . '.php';
	}

	$productId=$_GET['productId']; 
	$results = array();

	$productId = stripslashes($productId);
	$productId = mysql_real_escape_string($productId);
	$quantity = null;
	$status = true;

	$cart = NULL;
	if(!isset($_SESSION['cart'])){
		$cart = new OrderList(); 
	}else{
		$cart = $_SESSION['cart'];
	}
	
	if(!is_numeric($productId)) {
		$status = false;
	}
	
	if($status) {
		if (isset($cart->getOrderList()[$productId])){
			$order = $cart->getOrderList()[$productId];
			$order->setQuantity($order->getQuantity() + 1);
		} else {
			$order = new Order();
			$order->setProductId($productId);
			$order->setQuantity(1);
			$cart->insertOrder($productId, $order);
		}
		$_SESSION['cart']=$cart;
		$results['#success'] = 'true';
		$results['ordercount'] = $cart->countOrder();;
	} else {
		$results['#success'] = 'false';
	}	
	if($status){
		header("Location: shoppingcart.php");
	} else {
		echo json_encode($results);
	}	
	ob_end_flush();
?>